package com.sunny.design.flyweight.jdk.string;

/**
 * String有常量池, 缓存了字符串常量
 */
public class StringTest {
    public static void main(String[] args) {
        String s1 = "hello";
        String s2 = "hello";
        String s3 = "he" + "llo"; //jvm在编译器就将常量合并了, 所以与s1.s2是一样的
        String s4 = "hel" + new String("lo");
        String s5 = new String("hello");
        String s6 = s5.intern(); //返回常量池中的字符串,地址值与s1一致
        String s7 = "h";
        String s8 = "ello";
        String s9 = s7 + s8;//jvm不会编译变量的值

        System.out.println(s1==s2);//true
        System.out.println(s1==s3);//true
        System.out.println(s1==s4);//false
        System.out.println(s1==s9);//false
        System.out.println(s4==s5);//false
        System.out.println(s1==s6);//true
    }
}
